package com.dcc.redisson;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass(Redisson.class)
@EnableConfigurationProperties(RedissonProperties.class)
public class RedissonAutoConfiguration {

    @Bean
    public RedissonClient getRedissonClient(RedissonProperties redissonProperties){
        Config config = new Config();
        String prefix = "redis://";

        if(redissonProperties.isSSL()){
            prefix = "rediss://";
        }

        config
            .useSingleServer()
            .setAddress(prefix + redissonProperties.getHost() + ":" + redissonProperties.getPort())
            .setConnectionPoolSize(30)
            .setIdleConnectionTimeout(redissonProperties.getTimeout());


        return Redisson.create(config);
    }

}
